<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>

<script>

    function minSwaps(arr) {
        const n = arr.length;
        let swaps = 0;

        // 统计白色元素的位置
        const whiteIndices = {};
        for (let i = 0; i < n; i++) {
            if (arr[i] === 'W') {
                whiteIndices[i] = true;
            }
        }

        // 从左到右遍历红色元素，将其交换到正确的位置
        for (let i = 0; i < n; i++) {
            if (arr[i] === 'R') {
                if (whiteIndices[i]) {
                    // 如果当前位置是白色元素，找到一个白色元素的位置进行交换
                    for (let j = i + 1; j < n; j++) {
                        if (arr[j] === 'W') {
                            // 交换
                            [arr[i], arr[j]] = [arr[j], arr[i]];
                            swaps++;
                            delete whiteIndices[i];
                            whiteIndices[j] = true;
                            break;
                        }
                    }
                }
            }
        }

        return swaps;
    }

    // 示例输入
    const arr = ['W', 'R', 'R', 'W'];
    const swaps = minSwaps(arr);
    console.log(swaps); // 输出：1


</script>
</html>